import {
  defineStore
}
from 'pinia'
import LoginApi from '@/api/login.js'

export const useloginStore = defineStore({
  id: 'login', // id必填，且需要唯一
  state: () => {
    return {
      userInfo: uni.getStorageSync('userInfo') ? uni.getStorageSync(
        'userInfo') : {},
      globalConfig: uni.getStorageSync('globalConfig') ? uni
        .getStorageSync('globalConfig') : [],
      appSetting: uni.getStorageSync('appSetting') ? uni.getStorageSync(
        'appSetting') : [],
      multiport: false
    }
  },
  // actions 用来修改 state
  actions: {
    login(userInfo) {
      uni.setStorageSync('userInfo', userInfo)
      this.userInfo = userInfo
    },
    logout() {
      uni.removeStorageSync('userInfo');
      uni.removeStorageSync("authToken");
      uni.removeStorageSync("emojiList");
      this.userInfo = {};

      uni.reLaunch({
        url: "/pages/login/index",
        success() {
          history.pushState(null, null, location.href);
        }
      })
    },
    async getGlobalConfig() {
      // LoginApi.getSystemInfo().then((res) => {
      //   this.globalConfig = res.data;
      //   uni.setStorageSync('globalConfig', res.data)
      //   uni.setStorageSync('hobby_list', res.data.hobby_list ?? [])
      // })
      const res = await LoginApi.getSystemInfo();
      this.globalConfig = res.data;
      uni.setStorageSync('globalConfig', res.data)
      uni.setStorageSync('hobby_list', res.data.hobby_list ?? [])
    },
    setAppSetting(setting) {
      uni.setStorageSync('appSetting', setting)
      this.appSetting = setting
    }
  }
})